'''
Created on 04/ott/2009

@author: syn
'''
from ui.GladeBase import GladeBase
from ui.ManageUser import ManageUser
#from ui.ManageAuthor import ManageAuthor
from ui import ADMIN, AUTHOR, BOOKSHOP, check_authorization
from utils.Environment import logger, envir
from ui.ManageBook import ManageBook
#from ui.ManageBookshop import ManageBookshop
#from ui.ManageProduct import ManageProduct
#from ui.ManageOrder import ManageOrder
#from ui.ManagePrint import ManagePrint
#from ui.ManageBookstats import ManageBookstats

class Main(GladeBase):
    
    def __init__(self):
        logger.debug("UI: Main instance created")
        GladeBase.__init__(self, 'main_window')
        self.current_frame = self.main_frame_alignment
        
        if check_authorization(envir.get_user(), AUTHOR):
            self.main_toolbar.remove(self.bookshops_button)
            self.main_toolbar.remove(self.stockhouse_button)
            self.main_toolbar.remove(self.orders_button)
            self.main_toolbar.remove(self.users_button)
            self.main_toolbar.remove(self.print_button)
            self.main_toolbar.remove(self.stat_button)
            self.main_toolbar.remove(self.toolbuttonsep1)
            self.main_toolbar.remove(self.toolbuttonsep2)
            self.main_toolbar.remove(self.toolbuttonsep3)
        elif check_authorization(envir.get_user(), BOOKSHOP):
            self.main_toolbar.remove(self.authors_button)
            self.main_toolbar.remove(self.books_button)
            self.main_toolbar.remove(self.stockhouse_button)
            self.main_toolbar.remove(self.users_button)
            self.main_toolbar.remove(self.print_button)
            self.main_toolbar.remove(self.stat_button)
            self.main_toolbar.remove(self.toolbuttonsep2)
            self.main_toolbar.remove(self.toolbuttonsep3)

    def _retrieve_components(self):
        self.main_frame_alignment = self.builder.get_object('main_frame_alignment')
        self.get_from_builder('main_toolbar')
        self.get_from_builder('authors_button')
        self.get_from_builder('books_button')
        self.get_from_builder('bookshops_button')
        self.get_from_builder('stockhouse_button')
        self.get_from_builder('orders_button')
        self.get_from_builder('users_button')
        self.get_from_builder('print_button')
        self.get_from_builder('stat_button')
        self.get_from_builder('toolbuttonsep1')
        self.get_from_builder('toolbuttonsep2')
        self.get_from_builder('toolbuttonsep3')
        
    # Signals callbacks
    def on_users_button_clicked(self, object, data=None):
        frame = self.current_frame.get_child()
        logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
        self.current_frame.remove(frame)
        self.current_frame.add(ManageUser(top_component=self.root_component).root_component)
        
    #def on_authors_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManageAuthor(top_component=self.root_component).root_component)
    #    
    def on_books_button_clicked(self, object, data=None):
        frame = self.current_frame.get_child()
        logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
        self.current_frame.remove(frame)
        
        if check_authorization(envir.get_user(), AUTHOR):
            pass
            ## implement using type
            #author = envir.get_user().subject
            #self.current_frame.add(ManageBookstats(author=author, 
            #                                       top_component=self.root_component).root_component)
        else:
            self.current_frame.add(ManageBook(top_component=self.root_component).root_component)
        
    #def on_bookshops_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManageBookshop(top_component=self.root_component).root_component)
    #    
    #def on_stockhouse_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManageProduct(top_component=self.root_component).root_component)
    #
    #def on_orders_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManageOrder(top_component=self.root_component).root_component)
    #    
    #def on_print_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManagePrint(top_component=self.root_component).root_component)
    #    
    #def on_stat_button_clicked(self, object, data=None):
    #    frame = self.current_frame.get_child()
    #    logger.debug("working with main instance " +str(self)+ " hash: " +str(self.__hash__()))
    #    self.current_frame.remove(frame)
    #    self.current_frame.add(ManageBookstats(author=None, 
    #                                           top_component=self.root_component).root_component)
        
    def on_about_menuitem_activate(self, object, data=None):
        aboutdialog = GladeBase('about_dialog')
        aboutdialog.root_component.run()
        aboutdialog.destroy()
